import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class DiyService extends BaseService<any> {
  private readonly logger = new Logger(DiyService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getInfo
   * 对应 PHP: DiyService_api::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(params: any[]) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $start_up_page = [];         $page_template = [];          if (!empty($params[ 'name' ])) {              // 查询启动页             $diy_config_service = ne...
const start_up_page = [];
        const page_template = [];

        if (!!params[ 'name' ))) {

            // 查询启动页
            const diy_config_service = this.diyConfigService;
            const start_up_page = diy_config_service.getStartUpPageConfig(params[ 'name' )];

            const page_template = TemplateDict.getTemplate([ key: [ params[ 'name' ] ) )];
            if (!!page_template)) {
                const page_template = page_template [ params[ 'name' ] ];
            }
  }

  /**
   * getFirstPageData
   * 对应 PHP: DiyService_api::getFirstPageData()
   * 逻辑类型: undefined - undefined
   */
  async getFirstPageData(type: any) {
    // 基于PHP真实逻辑: getFirstPageData
    // PHP原文: $pages = PagesDict::getPages([ 'type' => $type ]);         if (!empty($pages)) {             $template = array_key_first($pages);             $page = ...
const pages = PagesDict.getPages([ type: type )];
        if (!!pages)) {
            const template = array_key_first(pages];
            const page = array_shift(pages];
            page[ 'template' ] = template;
            page[ 'type' ) = type;
            return page;
  }

  /**
   * handleThumbImgs
   * 对应 PHP: DiyService_api::handleThumbImgs()
   * 逻辑类型: undefined - undefined
   */
  async handleThumbImgs(data: any) {
    // 基于PHP真实逻辑: handleThumbImgs
    // PHP原文: $data = json_decode($data, true);          // todo $data['global']          foreach ($data[ 'value' ] as $k => $v) {              // 图片广告             ...
const data = json_decode(data, true);

        // todo data.global

        foreach (data[ 'value' ) as k => v) {

            // 图片广告
            if (v[ 'componentName' ) == 'ImageAds') {
                foreach (v[ 'list' ) as ck => cv) {
                    if (!!cv[ 'imageUrlThumbMid' ))) {
                        data[ 'value' ][ k ][ 'list' ][ ck ][ 'imageUrl' ] = cv[ 'imageUrlThumbMid' ;
  }
            }

            // 图文导航
            if (v[ 'componentName' ) == 'GraphicNav') {

                foreach (v[ 'list' ) as ck => cv) {
                    if (!!cv[ 'imageUrlThumbMid' ))) {
                        data[ 'value' ][ k ][ 'list' ][ ck ][ 'imageUrl' ] = cv[ 'imageUrlThumbMid' ;
  }
            }
  }

  /**
   * getDiyTheme
   * 对应 PHP: DiyService_api::getDiyTheme()
   * 逻辑类型: undefined - undefined
   */
  async getDiyTheme() {
    // 基于PHP真实逻辑: getDiyTheme
    // PHP原文: $site_addon = ( new CoreSiteService() )->getSiteCache($this->site_id);         $addon_list = array_merge($site_addon[ 'apps' ], $site_addon[ 'site_add...
const site_addon = ( this.coreSiteService ).getSiteCache(this.site_id];
        const addon_list = Object.assign(site_addon[ 'apps' ), site_addon[ 'site_addons' )];
        const theme_data = ( this.diyThemeService ).where([ [ 'site_id', '=', this.site_id ], [ 'is_selected', '=', 1 ) )).column('id,title,theme,new_theme', 'addon');
        const system_theme = array_values(array_filter(event('ThemeColor', [ key: 'app' ))))[ 0 ] || [];
        app_theme[ 'app' ] = [
            title: theme_data[ 'app' ][ 'title' ] || ( !!system_theme) ? system_theme[ 'theme_color' ][ 0 ][ 'title' ] : '' ),
            theme: theme_data[ 'app' ][ 'theme' ] || ( !!system_theme) ? system_theme[ 'theme_color' ][ 0 ][ 'theme' ] : '' ),
            new_theme: theme_data[ 'app' ][ 'new_theme' ] || '',
        ];
        const data = [];
        foreach (addon_list as key => value) {
//            if (typeof value[ 'support_app' )) && !value[ 'support_app' )) && value[ 'type' ] == 'addon') {
//                continue;
//            }
            const addon_theme = array_values(array_filter(event('ThemeColor', [ key: value[ 'key' ) ))))[ 0 ] || [];
            if (!!addon_theme) && !!addon_theme[ 'theme_color' ))) {
                data[ value[ 'key' ] ][ 'title' ] = theme_data[ value[ 'key' ] ][ 'title' ] || addon_theme[ 'theme_color' ][ 0 ][ 'title' ];
                data[ value[ 'key' ] ][ 'theme' ] = theme_data[ value[ 'key' ] ][ 'theme' ] || addon_theme[ 'theme_color' ][ 0 ][ 'theme' ];
                data[ value[ 'key' ] ][ 'new_theme' ] = theme_data[ value[ 'key' ] ][ 'new_theme' ] || '';
            }
  }
}
